<?php
/**
 * Created by PhpStorm.
 * User: 高鹏宇
 * Date: 2019/4/1
 * Time: 20:04
 */

namespace app\index\controller;


use app\index\model\User;
use think\captcha\Captcha;
use think\Controller;
use think\Request;
use think\Session;

class Login extends Controller
{
    protected $request;
    protected $user;

    public function __construct(Request $request = null)
    {
        parent::__construct($request);
        $this->request = Request::instance();
        $this->user = New User();
    }

    public function login()
    {
        return $this->fetch();
    }

    //验证码
    public function verify()
    {
        $captcha = New Captcha([
            'userCurve' => false
        ]);
        return $captcha->entry();
    }

    public function index()
    {
        return $this->fetch();
    }

    //注册
    public function zhuce()
    {
        if ($this->request->isGet()) {
            return $this->fetch();
        } else {
            $salt = getRandKey();
            $user_pwd = input('user_pwd');
            $salt_user_pwd = md5(md5($user_pwd) . $salt);
            $data = [
                'user_username' => input('user_username'),
                'user_pwd' => $salt_user_pwd,
                'user_name' => input('user_name'),
                'email' => input('email'),
                'salt' => $salt
            ];
            $res = $this->user->addData($data);
            if ($res) {
                $this->success('注册成功', 'login/denglu');
            } else {
                $this->error('注册失败', 'login/login');
            }
        }
    }

    //邮箱验证
    public function test()
    {
        $captcha = New Captcha([
            'userCurve' => false
        ]);
        $email = input('email');
        $captcha->entry();
        $code = Session::get('yzm');
//        var_dump($code);
//        die;
        $str = '';
        foreach ($code as $a) {
            $str .= $a;
        }
        $res = sendMail($email, '验证码邮件', '您的验证码为：' . $str);
        if ($res) {
            return json([
                'status' => 1,
                'msg' => '向您的邮箱发送一封邮件'
            ]);
        } else {
            return json([
                'status' => 0,
                'msg' => '发送失败'
            ]);
        }
    }

    //登录
    public function denglu()
    {
        //获取ajax提交过来的数据
        $user_username = input('user_username');
        $user_pwd = input('user_pwd');

        $model = new User();
        $where = [
            'user_username' => $user_username
        ];

        $node = $model->getOne($where);
        $user_pwd = md5(md5($user_pwd) . $node['salt']);
        if (is_null($node) == false && $node['user_pwd'] == $user_pwd) {
            Session::set("user_id", $node['user_id']);
            Session::set("user_username", $node['user_username']);
            return json([
                'status' => 1,
                'msg' => '登录成功'
            ]);
        } else {
            return json([
                'status' => 0,
                'msg' => '登录失败'
            ]);
        }
    }
}